import { Logger } from "../utils/Logger";
import { RetCodeDefine } from "../../config/RetCodeDefine";
import { RedisClient } from "../utils/RedisClient";
import { RedisManager } from "../utils/RedisManager";
import { RedisConfig } from "../../config/RedisConfig";
import { ZoneModel } from "../../model/ZoneModel";
import { UserModel } from "../../model/UserModel";
import { IpUtil } from "../utils/IpUtil";

export class WebReqHandler {

    public static instance: WebReqHandler = new WebReqHandler();

    public init(app) {
        // app.use("/serverlist", this.onGetServerList)
        app.use("/", this.onGetH5)
        app.use("/h2", this.onGetH5)
    }

    private checkActiveIp(ip: string) {
        //  17.0.0.0 ~ 17.255.255.255
        // 63.92.224.0 ~ 63.92.255.255
        // 65.199.22.0 ~ 65.199.23.255
        // 192.12.74.0 ~ 192.12.74.255
        // 192.42.249.0 ~ 192.42.249.255
        // 204.79.190.0 ~ 204.79.190.255
        // 144.178.0.0 ~ 144.178.63.255
        let ipArr: Array<string> = ip.split(".");
        if (ipArr.length >= 4) {
            if (ipArr[0] == "17") {
                return false;
            }
            else if (ipArr[0] == "63") {
                return false;
            }
            else if (ipArr[0] == "65") {
                return false;
            }
            else if (ipArr[0] == "192" && ipArr[1] == "42" && ipArr[2] == "249") {
                return false;
            }
            else if (ipArr[0] == "204" && ipArr[1] == "79" && ipArr[2] == "190") {
                return false;
            }
            else if (ipArr[0] == "144" && ipArr[1] == "178") {
                return false;
            }
            else {
                if(IpUtil.isInChina(ip)){
                    return true;
                }
                return false;
            }
        } else {
            return false;
        }
    }

    private async onGetH5(req, resp) {
        let ip = req.headers['X-Real-IP'] || req.connection.remoteAddress
        if (ip.indexOf('::ffff:') != -1) {
            ip = ip.substring(7);
        }
        Logger.log("req==", req)
        resp.setHeader("Access-Control-Allow-Origin", "*");
        let isIpActive: boolean = WebReqHandler.instance.checkActiveIp(ip);
        Logger.log("ip==", ip, isIpActive)
        if (req.originalUrl == "/h2") {
            resp.redirect(302, 'http://172.81.214.222:9601');
        } else {
            if (isIpActive) {
                resp.redirect(302, 'http://oauth.aiyinghun.com/fusion/app/?gid=120000123&cid=2&pkg=bnweb3&appid=110001188');
            } else {
                resp.redirect(302, 'http://212.64.108.20:9606/');
            }
        }
    }

    private async onGetServerList(req, resp) {
        Logger.log("onGetServerList==query", req.query)
        let ret: number = RetCodeDefine.OK;
        let result = {};
        if (req.query && req.query["userName"]) {
            let redis: RedisClient = RedisManager.instance.getRedis(RedisConfig.Database_Index.Users);
            let data = await redis.getValuePromise(ZoneModel.Redis_Key);
            result["serverList"] = data;
            let userName: string = req.query["userName"];
            let myData = await UserModel.getUserFromRedis(userName);
            Logger.log("userdatas==", myData)
            if (myData) {
                let obj: object = JSON.parse(myData);
                result["myServerList"] = obj["serverIdList"];
            } else {
                Logger.log("不存在该用户信息", userName)
            }
        } else {
            ret = RetCodeDefine.Error_Param;
        }
        result["ret"] = ret;
        result["msg"] = RetCodeDefine.code2Msg.get(ret);
        let resultStr: string = JSON.stringify(result)
        Logger.log("result==", resultStr);
        resp.end(resultStr);
    }
}